package com.world.tg.controller;

import com.world.tg.param.AddressParam;
import com.world.tg.param.RegisterOrLoginParam;
import com.world.tg.pojo.User;
import com.world.tg.result.BaseResult;
import com.world.tg.result.JsonResult;
import com.world.tg.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Part;
import java.io.IOException;

@RestController
@RequestMapping("/user")
@Api(tags = "用户模块")
@CrossOrigin("*")
public class UserController {

    @Autowired
    UserService userService;

    @GetMapping("/register")
    @ApiOperation("注册")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username",value = "用户名",required = true,paramType = "query"),
            @ApiImplicitParam(name = "password",value = "密码",required = true,paramType = "query")
    })
    public BaseResult register(
            @RequestParam String username,
            @RequestParam String password
    ){
        System.out.println(username);
        return userService.register(username,password);
    }



    @GetMapping("/login")
    @ApiOperation("登录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username",value = "用户名",required = true,paramType = "query"),
            @ApiImplicitParam(name = "password",value = "密码",required = true,paramType = "query")
    })
    public BaseResult login(
            @RequestParam String username,
            @RequestParam String password
    ){
        return userService.login(username,password);
    }


    @GetMapping("/isLogin")
    public JsonResult isLogin(){
        return userService.isLogin();
    }



    @PostMapping("/edit")
    @ApiOperation("修改用户信息")
    public BaseResult editUser(@RequestBody User user){
        return userService.editUser(user);
    }

    @GetMapping("/message")
    @ApiOperation("获取用户信息")
    public JsonResult getUserMsg(){
        return userService.findUserMsg();
    }

    @PostMapping("/addAddress")
    @ApiOperation("添加用户地址")
    public BaseResult doAddress(@RequestBody AddressParam param){
        return userService.addAddress(param);
    }

    @GetMapping("/address")
    @ApiOperation("查询用户的所有地址")
    public JsonResult findAddress(){
        return userService.findAddress();
    }

    @GetMapping("/collect")
    @ApiOperation("查询收藏夹")
    public JsonResult findCollect(){
        return userService.findCollect();
    }

    @GetMapping("/photo")
    @ApiOperation("修改头像")
    public JsonResult editPhoto(@RequestParam Part file){
        String filename = file.getSubmittedFileName();
        try{
            file.write(filename);
            return userService.editPhoto(filename);
        } catch (IOException e) {
            e.printStackTrace();
            JsonResult jsonResult = new JsonResult();
            jsonResult.fail("fail","修改失败");
            return jsonResult;
        }
    }

    @GetMapping("/getPhoto")
    @ApiOperation("获取头像")
    public JsonResult getPhoto(){
        return userService.getPhoto();
    }


    @GetMapping("/logout")
    @ApiOperation("退出登录")
    public BaseResult logout(){
        return userService.logout();
    }
}
